package com.imageim.yimirror.common.ui;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.imageim.yimirror.common.util.LogUtil;
 
/**
 * Created by Bruce.Zhou on 2018/8/15 10:30.
 * Email: 907160968@qq.com
 */
 
public class DraggablePhotoContainer extends SurfaceView implements Runnable, GestureDetector.OnGestureListener {
 
    private Paint paint;
    private SurfaceHolder holder;
    private boolean isDrawing;
    private Bitmap bitmap;
    private float x;
    private float y;
    private GestureDetector gestureDetector;
    private RectF rect1 = new RectF(30, 100, 130, 300);
    private RectF rect2 = new RectF(150, 100, 250, 300);
 
    private MoveOverListener moveOverListener;
    public interface MoveOverListener {
        void onMoveOver(int dropIndex);
    }
 
    public void setMoveOverListener(MoveOverListener listener) {
        moveOverListener = listener;
    }
 
    public DraggablePhotoContainer(Context context) {
        super(context);
        init();
    }
 
    public DraggablePhotoContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
 
    public DraggablePhotoContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
 
    private void init() {
        paint = new Paint();
        paint.setTextSize(40);
        paint.setAlpha(180);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(4);
        paint.setColor(Color.RED);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.STROKE);
//        paint.setDither(true);
        setZOrderOnTop(true);
        holder = getHolder();
        holder.setFormat(PixelFormat.TRANSLUCENT);
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                if (isDrawing == false) {
                    isDrawing = true;
                    new Thread(DraggablePhotoContainer.this).start();
                }
            }
 
            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
            }
 
            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                isDrawing = false;
            }
        });
        gestureDetector = new GestureDetector(getContext(), this);
        setEnabled(true);
        setClickable(true);
        setLongClickable(true);
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                LogUtil.d("draggable", motionEvent.getX() + "," + motionEvent.getY());
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        LogUtil.d("draggable", "ACTION_DOWN");
                        break;
                    case MotionEvent.ACTION_MOVE:
                        LogUtil.d("draggable", "ACTION_MOVE");
                        if (bitmap != null) {
                            x = motionEvent.getX() - bitmap.getWidth()/2;
                            y = motionEvent.getY() - bitmap.getHeight()/2;
                            if (moveOverListener != null) {
                                if (rect1.contains(x, y) && checkInToAreaSize(x, y, rect1)) {
                                    bitmap = null;
                                    moveOverListener.onMoveOver(0);
                                } else if (rect2.contains(x, y) && checkInToAreaSize(x, y, rect2)) {
                                    bitmap = null;
                                    moveOverListener.onMoveOver(1);
                                }
                            }
                        }
                    break;
                    case MotionEvent.ACTION_UP:
                        LogUtil.d("draggable", "ACTION_UP");
                        bitmap = null;
                        return false;
                    default:
                        break;
                }
//                return gestureDetector.onTouchEvent(motionEvent);
                if (bitmap == null) {
                    return false;
                }
                return true;
            }
        });
    }
 
    /**
     * 相对右下角坐标形成矩形面积是否大于一定值
     * @param x
     * @param y
     * @param rect
     * @return
     */
    private boolean checkInToAreaSize(float x, float y, RectF rect) {
        float size = Math.abs( x - rect.right) * Math.abs(y - rect.bottom);
        if (size > 2f/3 * rect.width() * rect.height()) {
            return true;
        }
        return false;
    }
 
    public void setBitmap(Bitmap bitmap, float x, float y) {
        this.bitmap = bitmap;
        this.x = x;
        this.y = y;
    }
 
    @Override
    public void run() {
        while (isDrawing) {
            draw();
        }
    }
 
    private void draw() {
        Canvas canvas = holder.lockCanvas();
        if (canvas == null) {
            return;
        }
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        canvas.drawRect(rect1, paint);
        canvas.drawRect(rect2, paint);
        if (bitmap!= null) {
            canvas.drawBitmap(bitmap, x, y, paint);
        }
        if (holder != null) {
            try {
                holder.unlockCanvasAndPost(canvas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        isDrawing = false;
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }
 
    @Override
    public boolean onDown(MotionEvent motionEvent) {
        LogUtil.d("draggable", "onDown");
        if (bitmap == null) {
            return false;
        }
        return true;
    }
 
    @Override
    public void onShowPress(MotionEvent motionEvent) {
 
    }
 
    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }
 
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        LogUtil.d("draggable", "onScroll " + distanceX + "," + distanceY);
        x -= distanceX;
        y -= distanceY;
        return false;
    }
 
    @Override
    public void onLongPress(MotionEvent motionEvent) {
 
    }
 
    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }
}